package app.excel.cartoon.year;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import app.esb.common.DBExcutor;
import cn.lily.com.container.data.Record;
import cn.lily.com.db.container.reader.DataTable;

/**
 * 动漫年份统计 - 数据处理类
 * @author JiaJ
 *
 */
public class ExcelYearData {
	
	/**
	 * year - cartoonDetailID
	 */
	private Map<String,List<String>> yearMap = new HashMap<String, List<String>>();
	
	/**
	 * cartoonDetailID - Record
	 */
	private Map<String,Record> cartoonMap = new HashMap<String,Record>();
	
	private DataTable dataTable;
	
	/**
	 * 装载数据
	 */
	public void instData(){
		
		String sql = "select detail.*,"
						  + "cartoon.CAN_NAME,cartoon.CAN_NAME_OTHER,cartoon.CAN_NAM_ENG,"
						  + "cartoon.CAN_AREA,cartoon.CAN_SOURCE,cartoon.CAN_FIRST,cartoon.CAN_DEMO,"
						  + "res.RES_STORE_PLACE,res.RES_STORE_SIZE,res.DISK_ID,res.RES_TAG_MEMO,cartoonTime.CAN_YEAR "
				   + "from LEO_CAN_DETAIL_INFO detail "
				   + "inner join LEO_CAN_INFO cartoon on detail.CAN_ID=cartoon.CAN_ID "
				   + "inner join LEO_RES_INFO res on detail.RES_ID=res.RES_ID "
				   + "inner join LEO_CAN_TIME cartoonTime on detail.CAN_DETAIL_ID=cartoonTime.CAN_DETAIL_ID "
				   + "order by detail.CAN_TIME ASC,detail.CAN_KIND DESC ";
		
		this.dataTable = DBExcutor.getDBManager().executeQueryTable(sql);
		
	}

	/**
	 * 分析数据
	 */
	public void excuteData(){
		
		while(this.dataTable.next()){
			
			String canDetailID = this.dataTable.getString("CAN_DETAIL_ID");
			String year = this.dataTable.getString("CAN_YEAR");
			
			//数据准备
			this.readyData(canDetailID, year);
			
			//集合放入
			this.cartoonMap.put(canDetailID, this.dataTable.getRecord());
			
		}
		
		System.out.println("数据分析完成..............");
		
	}
	
	/**
	 * 数据准备
	 * @param canID
	 * @param year
	 */
	private void readyData(String canDetailID,String year){
		
		if(!yearMap.containsKey(year)){
			List<String> yearList = new ArrayList<String>();
			this.yearMap.put(year, yearList);
		}
		
		
		List<String> cartoonList = this.yearMap.get(year);
		if(!cartoonList.contains(canDetailID)){
			cartoonList.add(canDetailID);
		}
	}
	
	/**
	 * 获取动漫标示集合
	 * @param word
	 * @return
	 */
	public List<String> getCartoonList(String word){
		return this.yearMap.get(word);
	}

	public Map<String, List<String>> getYearMap() {
		return yearMap;
	}
	
	public Record getCartoonDetail(String cartoonID){
		return this.cartoonMap.get(cartoonID);
	}
	
	public int getCartoonSize(String cartoonID){
		return this.getCartoonDetail(cartoonID).getInt("CAN_TOTAL");
	}
	
}
